﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DevExpress.XtraEditors;

using prjBULHRM;
namespace prjQuanLyNhanSu
{
    public partial class frmHopDong : XtraForm
    {
        public frmHopDong()
        {
            InitializeComponent();
        }
        clsBULHT_HopDong cls;
        public void LoadDuLieu()
        {
            cls = new clsBULHT_HopDong();
            dgrHopDong.DataSource = cls.SelectAll();
            txtMaHopDong.Enabled = false;
            txtNamHieuLuc.Enabled = false;
            txtTenHopDong.Enabled = false;
            txtFile.Enabled = false;
            btnFile.Enabled = false;

        }
        private void btnFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Multiselect = false;
            open.Filter ="Word 2000 document |*.doc|Word 2007 documet|*.docx|All File*.*|*.*";
            open.ShowDialog();
            string sFileName = open.FileName;
            if(sFileName!="")
            {
               string sFile= open.SafeFileName;
                   //sFileName.Substring(sFileName.LastIndexOf('/'),sFileName.Length-sFileName.LastIndexOf('/'));
                txtFile.Text=sFileName;
                File.Copy(sFileName,Application.StartupPath+"/File/HopDong/FileMau/"+sFile,true);
            }
        }

        private void frmHopDong_Load(object sender, EventArgs e)
        {
            LoadDuLieu();
        }

        private void dgrHopDong_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaHopDong.Enabled = false;
            txtTenHopDong.Enabled = false;
            txtNamHieuLuc.Enabled = false;
            txtFile.Enabled = false;
            btnFile.Enabled = false;
            btnSua.Enabled = true;
            btnThem.Enabled = true;
            btnXoa.Enabled = true;
        }

        private void dgrHopDong_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaHopDong.Text = dgrHopDong.CurrentRow.Cells[0].Value.ToString();
            txtTenHopDong.Text = dgrHopDong.CurrentRow.Cells[1].Value.ToString();
            txtNamHieuLuc.Text = dgrHopDong.CurrentRow.Cells[2].Value.ToString();
            txtFile.Text = dgrHopDong.CurrentRow.Cells[3].Value.ToString();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "&Thêm")
            {
                MessageBox.Show("Năm hi^ệu lực tính bằng tháng", "Th^ong báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                btnThem.Text = "&Lưu Thêm";
                txtMaHopDong.Text = "";
                txtTenHopDong.Text = "";
                txtNamHieuLuc.Text = "";
                txtFile.Text = "";
                txtMaHopDong.Enabled = true;
                txtTenHopDong.Enabled = true;
                txtNamHieuLuc.Enabled = true;
                txtFile.Enabled = true;
                btnFile.Enabled = true;
                txtMaHopDong.Focus();
                btnSua.Enabled = false;
                btnXoa.Enabled = false;
                txtFile.Enabled = false;
            }
            else
            {
                if (txtMaHopDong.Text == "" || txtTenHopDong.Text == "" || txtNamHieuLuc.Text == "" || txtFile.Text == "")
                {
                    MessageBox.Show("Bạn nhập thông tin chưa đầy đủ!", "Lỗi Dữ Liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    cls = new clsBULHT_HopDong();
                    btnThem.Text = "&Thêm";
                    cls.HopDongID = txtMaHopDong.Text;
                    cls.TenHopDong = txtTenHopDong.Text;
                    cls.NamHieuLuc =int.Parse(txtNamHieuLuc.Text);
                    cls.FileMau = txtFile.Text;
                    cls.Insert();
                    LoadDuLieu();
                    MessageBox.Show("Thêm thành công!");
                }
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text == "&Sửa")
            {
                if (txtMaHopDong.Text == "")
                {
                    MessageBox.Show("Bạn chưa chọn mẫu tin để sửa!", "Lỗi Dữ Liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    btnSua.Text = "&Lưu Sửa";
                    txtMaHopDong.Enabled = false;
                    txtTenHopDong.Enabled = true;
                    txtNamHieuLuc.Enabled = true;
                    txtFile.Enabled = true;
                    btnFile.Enabled = true;
                    btnThem.Enabled = false;
                    btnXoa.Enabled = false;
                    dgrHopDong.Enabled = false;
                    txtFile.Enabled = false;
                }
            }
            else
            {
                cls = new clsBULHT_HopDong();
                btnSua.Text = "&Sửa";
                cls.HopDongID = txtMaHopDong.Text;
                cls.TenHopDong = txtTenHopDong.Text;
                cls.NamHieuLuc =int.Parse(txtNamHieuLuc.Text);
                cls.FileMau = txtFile.Text;
                try
                {
                    if (txtNamHieuLuc.Text == "" || txtNamHieuLuc.Text == "" || txtFile.Text == "")
                    {
                        MessageBox.Show("Không được rỗng!", "Lỗi Dữ Liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        cls.Update();
                        LoadDuLieu();
                        MessageBox.Show("Sửa thành công!");
                        btnThem.Enabled = true;
                        btnXoa.Enabled = true;
                        dgrHopDong.Enabled = true;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Dữ liệu này không cho sửa!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                }
            }
        }

        private void txtNamHieuLuc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled=true;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {            
            if (txtMaHopDong.Text == "")
            {
                MessageBox.Show("Bạn chưa chọn dữ liệu để xóa!", "Lỗi Dữ Liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                DialogResult hoi = MessageBox.Show("Bạn có chắc muốn xóa dữ liệu này không?", "Xóa Dữ Liệu", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (hoi == DialogResult.Yes)
                {
                    try
                    {
                        cls = new clsBULHT_HopDong();
                        cls.HopDongID = txtMaHopDong.Text;
                        cls.Delete();
                        LoadDuLieu();
                        txtMaHopDong.Text = "";
                        txtNamHieuLuc.Text = "";
                        txtTenHopDong.Text = "";
                        MessageBox.Show("Xóa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Dữ liệu này không được xóa!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }                
            }
        }
    }
}
